package service

import (
	"chat_srv/basic/config"
	chat "chat_srv/basic/proto"
	"chat_srv/handler/dao"
	"context"
)

// 创建群聊
func (s *Server) CreateGroupMessage(_ context.Context, in *chat.CreateGroupMessageReq) (*chat.CreateGroupMessageResp, error) {
	// 业务逻辑
	group, err := dao.CreateGroupMessage(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.CreateGroupMessageResp{
		GroupId:   group.GroupId,
		GroupName: group.GroupName,
		Message:   "群聊创建成功",
	}, nil
}

// 加入群聊
func (s *Server) JoinGroup(_ context.Context, in *chat.JoinGroupReq) (*chat.JoinGroupResp, error) {
	// 业务逻辑
	group, err := dao.JoinGroup(config.Ctx, in)
	if err != nil {
		return nil, err
	}

	return &chat.JoinGroupResp{
		GroupId: group.GroupId,
		Message: "加入群聊成功",
	}, nil
}

// 发送群聊消息
func (s *Server) SendGroupMessage(_ context.Context, in *chat.SendGroupMessageReq) (*chat.SendGroupMessageResp, error) {
	// 业务逻辑
	groupMessage, err := dao.SendGroupMessage(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.SendGroupMessageResp{
		MessageId: groupMessage.ConversationId,
		Message:   "群聊消息发送成功",
	}, nil
}

// 获取群聊消息
func (s *Server) GetGroupMessage(_ context.Context, in *chat.GetGroupMessageReq) (*chat.GetGroupMessageResp, error) {
	// 业务逻辑
	groupMessage, err := dao.GetGroupMessage(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.GetGroupMessageResp{
		GroupMessages: groupMessage,
	}, nil
}

// 获取群成员
func (s *Server) GetGroupMember(_ context.Context, in *chat.GetGroupMemberReq) (*chat.GetGroupMemberResp, error) {
	// 业务逻辑
	members, err := dao.GetGroupMember(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.GetGroupMemberResp{
		Members: members,
	}, nil
}

// 查询用户所在群聊
func (s *Server) GetUserGroups(_ context.Context, in *chat.GetUserGroupsReq) (*chat.GetUserGroupsResp, error) {
	// 业务逻辑
	groupIds, err := dao.GetUserGroups(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.GetUserGroupsResp{
		GroupIds: groupIds,
	}, nil
}

// 聊天室推荐
func (s *Server) ChatRoomRecommend(_ context.Context, in *chat.ChatRoomRecommendReq) (*chat.ChatRoomRecommendResp, error) {
	// 业务逻辑
	group, err := dao.ChatRoomRecommend(config.Ctx, in)
	if err != nil {
		return nil, err
	}
	return &chat.ChatRoomRecommendResp{
		ChatRooms: group,
	}, nil
}
